<?php
if (!defined('IN_DISCUZ')){
	exit('Access Denied');
}

global $_G;

$aclist = array(
		'comment',
		'index',
		'get_category',
		'getAttachImgList',
		'deleteattach',
		'deleteproduct',
		'deactive',
		'active',
		'morepc',
		'like',
		'listlike',
		'addcart',
		'loadcart',
		'totalitemts',
		'delitem',
		'delcart',
		'district',
		'totalBill',
		'recount',
		'recountPrice',
		'payment'
);
$ac = !in_array($_G['gp_ac'], $aclist) ? 'index' : $_G['gp_ac']; //echo $ac;

switch ($ac){
	case 'payment':
		if (isset($_GET))
			$arr = $_GET;

		$listitems = cart::listCart($arr['sid']);
		$listitems = serialize($listitems);

		$money = $arr['totalprice'];

		$provice = provice();
		foreach ($provice as $key=>$value){
			if ($key == $arr['city']){
				$city = $value['name'];
			}
		}

		$info = array(
				'uid'=>intval($arr['sid']),
				'name'=>$arr['name'],
				'email'=>$arr['email'],
				'tel'=>$arr['tel'],
				'address'=>$arr['address'],
				'city'=>$city,
				'district'=>$arr['dist'],
				'note'=>$arr['note']
		);

		$res = cart::insertBill($arr['sid'], $info, $listitems, $money);

		include template('common/header_ajax');
		echo $res;
		include template('common/footer_ajax');
	break;
	case 'recountPrice':
		$pid = is_numeric($_G['gp_pid']) ? intval($_G['gp_pid']) : 0;
		$quantity = is_numeric($_G['gp_q']) ? intval($_G['gp_q']) : 0;
		$ud = $_G['gp_ud'] ? strval($_G['gp_ud']) : 'down';

		$res = cart::recountPrice($pid, $quantity);

		include template('common/header_ajax');
		echo $res;
		include template('common/footer_ajax');
	break;
	case 'recount':
		$sid = is_numeric($_G['gp_sid']) ? intval($_G['gp_sid']) : 0;

		$res = cart::recountBill($sid);

		include template('common/header_ajax');
		echo $res;
		include template('common/footer_ajax');
	break;
	case 'district':
		$did = is_numeric($_G['gp_did']) ? intval($_G['gp_did']) : 0;

		$Qry = 'SELECT name
				FROM ' . DB::table('common_district') . '
				WHERE upid = ' . $did . '
				ORDER BY name ASC';
		$res = DB::query($Qry);

		include template('common/header_ajax');
		$str = '<option value="">' . lang('shop/template', 'chon_quan_huyen') . '</option>';
		while ($row = DB::fetch($res)){
			$str .= '<option value="' . $row['name'] . '">' . $row['name'] . '</option>';
		}
		echo $str;
		include template('common/footer_ajax');
	break;
	case 'addcart':
		$cart = array();
		$status = false;

		$sid = is_numeric($_G['gp_sid']) ? intval($_G['gp_sid']) : 0;
		$pid = is_numeric($_G['gp_pid']) ? intval($_G['gp_pid']) : 0;
		$quantity = is_numeric($_G['gp_q']) ? intval($_G['gp_q']) : 1;

		if ($sid && $pid &&  $quantity){
			$cart['uid'] = $_G['uid']; // User ID
			$cart['sid'] = $sid; // Shop ID
			$cart['pid'] = $pid; // Product ID
			$cart['quantity'] = $quantity;
		}

		$status = cart::addShoppingCart($cart);

		include template('common/header_ajax');
		echo $status;
		include template('common/footer_ajax');
	break;
	case 'loadcart':
		$listCart = cart::listCart();
		$items = count($listCart);

		include template('common/header_ajax');
		include template('shop/detail/loadcart');
		include template('common/footer_ajax');
	break;
	case 'totalitemts':
		$number = cart::totalitemts();

		include template('common/header_ajax');
		echo $number;
		include template('common/footer_ajax');
	break;
	case 'delitem':
		$pid = is_numeric($_G['gp_pid']) ? intval($_G['gp_pid']) : 0;
		$sid = is_numeric($_G['gp_sid']) ? intval($_G['gp_sid']) : 0;

		$res = cart::deleteItem($pid, $sid);

		include template('common/header_ajax');
		echo $res;
		include template('common/footer_ajax');
	break;
	case 'totalBill':
		$sid = is_numeric($_G['gp_sid']) ? intval($_G['gp_sid']) : 0;

		$res = cart::totalBill($sid);

		include template('common/header_ajax');
		echo $res;
		include template('common/footer_ajax');
	break;
	case 'delcart':
		$sid = is_numeric($_G['gp_sid']) ? intval($_G['gp_sid']) : 0;

		$res = cart::deleteCart($sid);

		include template('common/header_ajax');
		echo $res;
		include template('common/footer_ajax');
	break;
	case 'listlike':
		$id = is_numeric($_G['gp_pid']) ? intval($_G['gp_pid']) : 0;
		$listlike = like::ListLikeItem($id, 'product');

		include template('common/header_ajax');
		include template('shop/detail/list_like');
		include template('common/footer_ajax');
	break;
	case 'like':
		$id = is_numeric($_G['gp_pid']) ? intval($_G['gp_pid']) : 0;
		$do = $_G['gp_do'] ? strval($_G['gp_do']) : '';

		if ($do == 'like')
			$str = like::likeItem($id, 'product');
		elseif ($do == 'unlike')
			$str = like::unLikeItem($id, 'product');

		include template('common/header_ajax');
		echo $str;
		include template('common/footer_ajax');
	break;
	case 'morepc':
		$id = is_numeric($_G['gp_pid']) ? intval($_G['gp_pid']) : 0;

		$comarray = proComment($id);

		include template('common/header_ajax');
		include template('shop/detail/all_comments');
		include template('common/footer_ajax');
	break;
	case 'comment':
		$id = is_numeric($_G['gp_id']) ? intval($_G['gp_id']) : 0;

		if ($id){
			$Qry = "SELECT *
					FROM " . DB::table('home_comment') . "
					WHERE cid='$id' AND idtype='product'
					LIMIT 0,1";
			$value = DB::fetch_first($Qry);

			include template('ajax/ajax_comment');
			dexit();
		}
	break;
	case 'get_category':
		$category_num = intval($_G['gp_numbox']);
		$shoplist = shop_menu($_G['gp_parent_id']);

		include template('common/header_ajax');
		include template('shop/store/combox_category');
		include template('common/footer_ajax');
	break;
	case 'getAttachImgList':
		$pid = isset($_G['gp_pid']) ? intval($_G['gp_pid']) : 0; // Product id
		$dimg = isset($_G['gp_dimg']) ? intval($_G['gp_dimg']) : 0; // is image cover product


		require_once libfile('function/post');
		require_once libfile('function/shop');

		$attachlist = getattachshop($pid);
		$imagelist = $attachlist['imgattachs']['unused'];
		$imagelistused = $attachlist['imgattachs']['used'];

		include template('common/header_ajax');
		include template('shop/store/image_upload_list');
		include template('common/footer_ajax');
		dexit();
	break;
	case 'deleteattach':
		$str = '';
		$id = isset($_G['gp_id']) ? intval($_G['gp_id']) : 0;
		$pid = isset($_G['gp_pid']) ? intval($_G['gp_pid']) : 0;

		if ($id){
			if ($pid){
				$attach = DB::fetch_first("SELECT * FROM " . DB::table("shop_products_attachment") . " WHERE aid = " . $id . " AND pid = " . $pid);

				if ($_G['adminid'] == 1 || ($attach && $_G['uid'] == $attach['uid'])){
					DB::delete('shop_products_attachment', "aid='$id'");
					punlink($attach, 'update', true);
					$str = 'deleted';
				}
			}else{
				$attach = DB::fetch_first("SELECT * FROM " . DB::table(getattachtablebyaid($id)) . " WHERE aid='$id'");

				if ($attach && $_G['uid'] == $attach['uid']){
					DB::delete(getattachtablebyaid($id), "aid='$id'");
					DB::delete('forum_attachment', "aid='$id'");
					punlink($attach);
					$str = 'deleted';
				}
			}
		}
		include template('common/header_ajax');
		echo $str;
		include template('common/footer_ajax');
		dexit();
	break;
	case 'deactive':
		$pid = $_G['gp_pid'] ? intval($_G['gp_pid']) : 0;
		$number = 0;

		if ($pid){
			$row = DB::fetch_first("SELECT uid,pid,available FROM " . DB::table("shop_products") . " WHERE pid = " . $pid);
			//Check product
			if ($_G['adminid'] == 1 || $_G['uid'] == $row['uid']){
				if ($row['available'] == 1){
					DB::update("shop_products", array(
							'available'=>0
					), array(
							'pid'=>$row['pid']
					));
					$number = 0;
				}
			}
		}
		include template('common/header_ajax');
		echo $number;
		include template('common/footer_ajax');
		dexit();
	break;
	case 'active':
		$pid = $_G['gp_pid'] ? intval($_G['gp_pid']) : 0;
		$number = 0;

		if ($pid){
			$row = DB::fetch_first("SELECT uid,pid,available FROM " . DB::table("shop_products") . " WHERE pid = " . $pid);
			//Check product
			if ($_G['adminid'] == 1 || $_G['uid'] == $row['uid']){
				if ($row['available'] == 0){
					DB::update("shop_products", array(
							'available'=>1
					), array(
							'pid'=>$row['pid']
					));
					$number = 1;
				}
			}
		}
		include template('common/header_ajax');
		echo $number;
		include template('common/footer_ajax');
		dexit();
	break;
	case 'deleteproduct':
		$pid = $_G['gp_pid'] ? intval($_G['gp_pid']) : 0;
		$str = '';

		if ($pid){
			$row = DB::fetch_first("SELECT * FROM " . DB::table("shop_products") . " WHERE pid = " . $pid);
			//Check product
			if ($_G['adminid'] == 1 || $_G['uid'] == $row['uid']){
				DB::update("shop_products", array(
						'hide'=>1
				), array(
						'pid'=>$row['pid']
				));
				$str = 'deleted';
			}
		}
		include template('common/header_ajax');
		echo $str;
		include template('common/footer_ajax');
		dexit();
	break;
}